

package com.example.myapplication.fragment;

import android.os.Bundle;

import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myapplication.R;
import com.example.myapplication.adapter.Servlet;
import com.example.myapplication.adapter.Servlet_02;
import com.example.myapplication.data.HomeServlet;
import com.example.myapplication.utils.OkHttpTool;

import java.util.ArrayList;
import java.util.List;


public class ServletFragment extends Fragment {
    private List<HomeServlet.RowsDTO> list;
    private View view;
    private Handler handler;
    private RecyclerView servlet;
    private SearchView sear;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_servler, container, false);
        handler = new Handler();
        sear = view.findViewById(R.id.sear);
        servlet = view.findViewById(R.id.servlet);
        page();
        sear_();
        return view;
    }

    private void sear_() {
        sear.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                updateSearchResults(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                search(newText);
                return true;
            }
        });
    }
    private void updateSearchResults(String query) {

        if(!"".equals(query) && !TextUtils.isEmpty(query)){
            page02(query);
        }else if("".equals(query.trim())){
            page();
        }
    }
    private void search(String query) {

        if (!"".equals(query) && !TextUtils.isEmpty(query)) {
            page02(query);
        } else if ("".equals(query.trim())) {
            page();
        }

    }
    private void page02(String query) {
        String url = "prod-api/api/service/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, HomeServlet.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                HomeServlet homeServlet = (HomeServlet)obj;

                list = homeServlet.getRows();
                List<HomeServlet.RowsDTO> list01 = new ArrayList<>();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        list.add(new HomeServlet.RowsDTO(50,"律师咨询"));
                        list.add(new HomeServlet.RowsDTO(51,"预约检车"));
                        for (HomeServlet.RowsDTO rowsDTO : list) {
                            if(rowsDTO.getServiceName().equals(query)){
                                list01.add(rowsDTO);
                            }
                        }
                        pageAdapter02(list01);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }
    private void page() {
        String url = "prod-api/api/service/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, HomeServlet.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
               HomeServlet homeServlet = (HomeServlet)obj;

               list = homeServlet.getRows();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        list.add(new HomeServlet.RowsDTO(50,"律师咨询"));
                        list.add(new HomeServlet.RowsDTO(51,"预约检车"));
                        pageAdapter();
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void pageAdapter() {
        Servlet_02 servlet02 = new Servlet_02(list,ServletFragment.this.getContext());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(ServletFragment.this.getContext(),5);
        servlet.setLayoutManager(gridLayoutManager);
        servlet.setAdapter(servlet02);

    }
    private void pageAdapter02(List<HomeServlet.RowsDTO> list02) {
        Servlet_02 servlet02 = new Servlet_02(list02,ServletFragment.this.getContext());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(ServletFragment.this.getContext(),5);
        servlet.setLayoutManager(gridLayoutManager);
        servlet.setAdapter(servlet02);

    }


}